package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.CmfzUser;
import com.baizhi.service.CmfzUserService;
import com.baizhi.util.ThreadPollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
    @RequestMapping("user")
public class CmfzUserController {
    @Autowired
    private CmfzUserService cmfzUserService;
    @RequestMapping("selectAllUsers")
    @ResponseBody
    @LogAnnotation(content = "查询所有用户信息",type = "查询")
    public Map selectAll(Integer page,Integer limit,String userTelphone){
        HashMap map = new HashMap();
        Page<CmfzUser> userPage = cmfzUserService.selectAll(page, limit, userTelphone);
        map.put("code",0);
        map.put("msg","");
        map.put("count",userPage.getTotal());
        map.put("data",userPage.getRecords());
        return map;
    }
    @RequestMapping("updateUserStatus")
    @ResponseBody
    @LogAnnotation(content = "更新用户状态",type = "更新")
    public Map updateUserStatus(CmfzUser cmfzUser){
        System.out.println(cmfzUser);
        HashMap map = new HashMap();
        try{
            if (cmfzUser.getUserStatus()==0){
                cmfzUser.setUserStatus(1);
                int updateUserStatus = cmfzUserService.updateUserStatus(cmfzUser);
                if (updateUserStatus>0){
                    map.put("flag",true);
                }else{
                    map.put("flag",false);
                }
            }else {
                cmfzUser.setUserStatus(0);
                int updateUserStatus = cmfzUserService.updateUserStatus(cmfzUser);
                if (updateUserStatus>0){
                    map.put("flag",true);
                }else{
                    map.put("flag",false);
                }
            }
        } catch (Exception e){
            map.put("flag",false);
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("selectCountBySex")
    @LogAnnotation(content = "查询男女比例图",type = "查询")
    public List<Integer> selectCountBySex(){
        List<Integer> integers = cmfzUserService.selectCountBySex();
        return integers;
    }
    @RequestMapping("selectCountByProvince")
    @ResponseBody
    @LogAnnotation(content = "查询用户在中国地图上的分布",type = "查询")
    public List<Map> selectCountByProvince(){
        List<Map> countByProvinces = cmfzUserService.selectCountByProvince();
        return countByProvinces;
    }
    @RequestMapping("selectCountByDay")
    @ResponseBody
    @LogAnnotation(content = "查询最近几个星期用户注册量",type = "查询")
    public Map selectCountByDay(){
        return cmfzUserService.selectCountByDay();
    }
    /*
    * 不带优化的一个Ajax查询全部图表
    * */
    @ResponseBody
    @RequestMapping("selectAllNo")
    @LogAnnotation(content = "查询不带优化的三个图",type = "查询")
    public Map selectAllNo(){
        long start = System.currentTimeMillis();
        Map map = cmfzUserService.selectAllNo();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;
    }
    /*带优化的一个Ajax查询全部图表*/
    @RequestMapping("selectAllYes")
    @ResponseBody
    @LogAnnotation(content = "查询带优化的三个图",type = "查询")
    public Map selectAllYes() throws InterruptedException {
        long start = System.currentTimeMillis();
        Map map = cmfzUserService.selectAllYes();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;
    }
}
